<script setup lang="ts">
import { ref, watch } from "vue";
import { Loading, ChatLineRound } from "@element-plus/icons-vue";
import type { Comment } from "@/api/interface/comment";

interface Props {
  visible: boolean;
  comments: Comment.CommentItem[];
  currentUserId?: string;
  commentText?: string;
  commentLoading: boolean;
  currentDocId?: string;
}

const props = defineProps<Props>();

const emit = defineEmits<{
  (e: "update:commentText", value: string): void;
  (e: "submit"): void;
  (e: "reply", comment: Comment.CommentItem): void;
  (e: "delete", commentId: string): void;
}>();

const localComments = ref<Comment.CommentItem[]>([]);

watch(
  () => props.comments,
  newVal => {
    localComments.value = Array.isArray(newVal) ? [...newVal] : [];
    console.log("Comments updated:", localComments.value);
  },
  { immediate: true, deep: true }
);

const formatTimeAgo = (timeStr: string): string => {
  if (!timeStr) return "刚刚";
  try {
    const now = new Date();
    const time = new Date(timeStr);
    const diff = now.getTime() - time.getTime();
    const minutes = Math.floor(diff / 60000);
    if (minutes < 60) return `${minutes}分钟前`;
    const hours = Math.floor(minutes / 60);
    if (hours < 24) return `${hours}小时前`;
    const days = Math.floor(hours / 24);
    return `${days}天前`;
  } catch {
    return timeStr;
  }
};
</script>

<template>
  <div class="comments-panel-container" v-if="visible">
    <div class="comments-panel-header">
      <h3>评论</h3>
    </div>

    <div v-if="commentLoading && localComments.length === 0" class="empty-comments-state">
      <el-icon class="empty-comments-icon"><Loading /></el-icon>
      <span>加载评论中...</span>
    </div>

    <template v-else>
      <div class="comments-list-container">
        <template v-if="commentLoading && localComments.length > 0">
          <div class="comment-loading-overlay">
            <el-icon class="loading-icon"><Loading /></el-icon>
            <span>发送中...</span>
          </div>
        </template>

        <div class="comment-item-container" v-for="comment in localComments" :key="comment.FId">
          <div class="comment-avatar-wrapper">
            <el-avatar :size="36" :src="comment.FAvatar" />
          </div>
          <div class="comment-content-wrapper">
            <div class="comment-header-wrapper">
              <div class="comment-user-info">
                <span class="comment-username">{{ comment.FUsername || comment.FUserId }}</span>
                <span class="comment-time">{{ formatTimeAgo(comment.FCreatedAt!) }}</span>
              </div>
            </div>
            <div class="comment-text-content">{{ comment.FContent }}</div>
            <div class="comment-actions-wrapper">
              <el-link
                type="primary"
                :underline="false"
                @click="emit('reply', comment)"
                :disabled="!currentUserId || commentLoading"
              >
                回复
              </el-link>
              <el-link
                type="danger"
                :underline="false"
                v-if="comment.FUserId === currentUserId"
                @click="emit('delete', comment.FId)"
                :disabled="commentLoading"
              >
                删除
              </el-link>
            </div>
          </div>
        </div>

        <div class="empty-comments-state" v-if="localComments.length === 0 && !commentLoading">
          <el-icon class="empty-comments-icon"><ChatLineRound /></el-icon>
          <span>暂无评论</span>
        </div>
      </div>

      <div class="comment-editor-container" v-if="currentDocId">
        <el-input
          :model-value="commentText"
          @update:model-value="val => emit('update:commentText', val)"
          type="textarea"
          :rows="3"
          placeholder="发表你的评论..."
          :disabled="!currentUserId || commentLoading"
        />
        <div class="comment-editor-actions">
          <el-button
            size="small"
            type="primary"
            @click="emit('submit')"
            :loading="commentLoading"
            :disabled="!commentText?.trim()"
          >
            {{ commentLoading ? "发送中..." : "提交" }}
          </el-button>
        </div>
      </div>
    </template>
  </div>
</template>

<style scoped>
.comments-panel-container {
  display: flex;
  flex-direction: column;
  height: 100%;
  background-color: #ffffff;
}
.comments-panel-header {
  padding: 16px 20px;
  border-bottom: 1px solid #f0f0f0;
}
.comments-panel-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: #333333;
}
.comments-list-container {
  position: relative;
  flex: 1;
  padding: 16px;
  overflow-y: auto;
}
.comment-item-container {
  display: flex;
  padding: 12px 16px;
  margin-bottom: 16px;
  background: #f9fafc;
  border-radius: 8px;
  transition: all 0.2s ease;
}
.comment-item-container:hover {
  background: #f1f5f9;
}
.comment-avatar-wrapper {
  margin-right: 12px;
}
.comment-content-wrapper {
  flex: 1;
}
.comment-header-wrapper {
  margin-bottom: 8px;
}
.comment-user-info {
  display: flex;
  gap: 8px;
  align-items: center;
}
.comment-username {
  font-size: 14px;
  font-weight: 600;
  color: #333333;
}
.comment-time {
  font-size: 12px;
  color: #999999;
}
.comment-text-content {
  margin-bottom: 8px;
  font-size: 14px;
  line-height: 1.6;
  color: #333333;
}
.comment-actions-wrapper {
  display: flex;
  gap: 16px;
  margin-top: 8px;
}
.comment-actions-wrapper .el-link {
  font-size: 12px;
}
.comment-editor-container {
  position: sticky;
  bottom: 0;
  padding: 16px;
  background: #ffffff;
  border-top: 1px solid #f0f0f0;
}
.comment-editor-actions {
  margin-top: 12px;
  text-align: right;
}
.empty-comments-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 40px;
  color: #999999;
  text-align: center;
}
.empty-comments-icon {
  margin-bottom: 16px;
  font-size: 48px;
  color: #dddddd;
}
.comment-loading-overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: rgb(255 255 255 / 80%);
}
.comment-loading-overlay .loading-icon {
  margin-bottom: 8px;
  font-size: 24px;
  animation: rotating 2s linear infinite;
}

@keyframes rotating {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
</style>
